package main

import (
	"fmt"
	"sync"
	"sync/atomic"
)

type AdvancedCounter struct {
	count int64
}

func (c *AdvancedCounter) Increment() int64 {
	return atomic.AddInt64(&c.count, 1)
}

func (c *AdvancedCounter) Decrement() int64 {
	return atomic.AddInt64(&c.count, -1)
}

func (c *AdvancedCounter) Value() int64 {
	return atomic.LoadInt64(&c.count)
}

func (c *AdvancedCounter) Reset() int64 {
	return atomic.SwapInt64(&c.count, 0)
}

func main() {
	var counter AdvancedCounter
	var wg sync.WaitGroup

	// 并发增加
	for i := 0; i < 500; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			counter.Increment()
		}()
	}

	// 并发减少
	for i := 0; i < 300; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			counter.Decrement()
		}()
	}

	wg.Wait()
	fmt.Println("Final count:", counter.Value()) // 输出: Final count: 200

	// 重置计数器
	oldValue := counter.Reset()
	fmt.Println("Reset value:", oldValue)          // 输出: Reset value: 200
	fmt.Println("Current value:", counter.Value()) // 输出: Current value: 0
}
